{layout name="public/layout" /}
{include file='yxnavbar'}


<div class="main">
    {include file='sms/sms_header'}
    <div class="panel panel-info">
        <div class="panel-heading">筛选 共{$count}条
            <span style="color: red;margin-left: 10px;">
                当前可用余额:{$amount_total}元 = 充值{$amount_array[1]['sum']}元 - 提现{php echo abs($amount_array[2]['sum']);}元 - <a href="{php echo createWebUrl('sms',array('op' => 'sendlog','themeid'=>$themeid));}">【消费{$used_amount}元】(包含发送中的暂扣)</a>
            </span>
        </div>
        <div class="panel-body">
            <form action="./index.php" method="get" class="form-horizontal" role="form">
                <input type="hidden" name="c" value="site" />
                <input type="hidden" name="a" value="entry" />
                <input type="hidden" name="m" value="{$_P['m']}" />
                <input type="hidden" name="do" value="{$do}" />
                <input type="hidden" name="ac" value="search" />
                <input type="hidden" name="themeid" value="{$themeid}" />

                <div class="form-group">
                    <label class="col-xs-1 col-sm-1 col-md-1 control-label">类型</label>
                    <div class="col-xs-11 col-sm-9 col-md-9">
                        <div class="btn-group">
                            {loop $amount_array $row}
                            <a href="{php echo createWebUrl('sms',array('op' => 'amount_list','themeid'=>$themeid,'type'=>$row['key']));}"
                               class="btn {if $type == $row['key']}btn-primary{else}btn-default{/if}">
                                {$row['title']}
                                <span style="color: #ff0000">{$row['total']}</span>
                            </a>
                            {/loop}
                        </div>
                    </div>
                </div>
                <div class="form-group">
                    <label class="col-xs-12 col-sm-3 col-md-1 control-label">关键字</label>
                    <div class="col-sm-9 col-xs-2 col-md-3">
                        <input class="form-control" name="search_data" placeholder="短信模版使用说明" type="text" value="{$_P['search_data']}">
                    </div>
                    <div class="col-sm-3 col-lg-3 col-md-3 col-xs-6">
                        <button class="btn btn-default"><i class="fa fa-search"></i> 搜索</button>
                        <input type="hidden" name="__token__" value="{$Request.token}"/>
                    </div>
                    {if $_G['isSuperAdmin']}
                    <div class="col-sm-3 col-lg-3 col-md-3 col-xs-6">
                        <button  type="button"  id="amount_recharge" class="btn btn-success">充值</button>
                        <button  type="button"  id="amount_cashout"  class="btn btn-warning">提现</button>
                    </div>
                    {/if}
                </div>
            </form>
        </div>
    </div>

    <div class="tab-content">
        <div class="panel panel-default">
            <div class="panel-body table-responsive">
                <table class="table table-hover">
                    <thead class="navbar-inner">
                    <tr>
                        <th style="width: 80px;">序号</th>
                        <th style="width: 120px;">类型</th>
                        <th style="width: 100px;">充值金额</th>
                        
                        <th style="width: 200px;">备注</th>
                        <th style="width: 80px;">操作人</th>
                        <th style="width: 80px;">操作ip</th>
                        <th style="width: 140px;">创建时间</th>
<!--                        <th style="width: 140px;">操作</th>-->
                    </tr>
                    </thead>
                    <tbody>
                    {loop $list $item}
                    <tr>
                        <td>{$item['id']}</td>
                        <td>
                            {if $item['type']==1}
                              <span class="label label-success">充值</span>
                            {elseif $item['type']==2}
                              <span class="label label-warning">消费</span>
                            {elseif $item['type']==3}
                              <span class="label label-default">提现</span>
                            {/if}
                        </td>
                        <td>
                            {$item['amount']}
                        </td>
                        
                        <td>{$item['remark']}</td>
                        <td>{$item['admin_name']}</td>
                        <td>{$item['ip']}</td>
                        <td>{$item['created_at']}</td>
<!--                        <td>-->
<!--                            <a class="btn btn-danger btn-sm" onclick="showWaitSend(this)" data-id="{$item['id']}" title="队列发送"><i class="icon-edit"></i>立即发送</a>-->
<!--                        </td>-->
                    </tr>
                    {/loop}
                    </tbody>
                </table>
            </div>
        </div>
    </div>
    {$pager}
    <div id="show_form1" style="display:none;">
        <form action="#" method="post" class="layui-form" role="form" name="show_form1" id="recharge_form">
            <input type="hidden" name="themeid" value="{$themeid}"/>
            <input type="hidden" name="img" id="img" value=""/>
            <div class="layui-form-item" style="margin-top: 30px;">
                <label class="layui-form-label">金额</label>
                <div class="layui-input-inline">
                    <input type="number" class="layui-input"  placeholder="请输入充值金额" name="amount"  id="amount" value="">
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">凭证</label>
                <div class="layui-input-block">
                    <button type="button" class="layui-btn layui-btn-sm" id="ID-upload-demo-btn" >选择文件</button>
                    <div style="width: 132px;">
                        <div class="layui-upload-list">
                            <img class="layui-upload-img" id="ID-upload-demo-img" style="width: 100%; height: 92px;">
                            <div id="ID-upload-demo-text"></div>
                        </div>
                        <div class="layui-progress layui-progress-big" lay-showPercent="yes" lay-filter="filter-demo">
                            <div class="layui-progress-bar" lay-percent=""></div>
                        </div>
                    </div>
                </div>
            </div>
            <input type="hidden" name="upload_img_url" id="upload_img_url">
            <div class="form-group ml100"  >
                <div class="col-sm-12">
                    <input id="addRecharge"   type="button" value="提交" class="btn btn-primary col-lg-2">
                </div>
            </div>
        </form>
    </div>

    <div id="show_form2" style="display:none;">
        <form action="#" method="post" class="layui-form" role="form" name="show_form1" id="cashout_form">
            <input type="hidden" name="themeid" value="{$themeid}"/>
            <div class="layui-form-item" style="margin-top: 40px;">
                <label class="layui-form-label">金额</label>
                <div class="layui-input-inline">
                    <input type="number" class="layui-input"  placeholder="请输入提现金额" name="amount"   value="">
                </div>
            </div>
            <div class="form-group ml100"  >
                <div class="col-sm-12">
                    <input id="addCashout"   type="button" value="提交" class="btn btn-primary col-lg-2">
                </div>
            </div>
        </form>
    </div>

</div>

<script>
    var send_num=1;
    var timer;
    var success_total =0;
    var fail_total =0;
    layui.use(['form','layer','upload'], function() {
        var form = layui.form;
        var upload = layui.upload;
        var element = layui.element;
        var upload_token = "{$Request.token}";
        $("#amount_recharge").on("click",function () {
            var index = layer.open({
                type:1,
                title:['添加充值记录'],
                area:['600px','440px'],
                content:$('#show_form1')
            })
        });

        $("#amount_cashout").on("click",function () {
            var index = layer.open({
                type:1,
                title:['添加提现记录'],
                area:['600px','240px'],
                content:$('#show_form2')
            })
        });
        let UploadPostUrl = "{php echo createWebUrl('sms',array('op'=>'amount_upload','themeid'=>$themeid));}";
        upload.render({
            elem: '#ID-upload-demo-btn',
            url: UploadPostUrl, // 此处用的是第三方的 http 请求演示，实际使用时改成您自己的上传接口即可。
            data:{token:upload_token},
            before: function(obj){
                // 预读本地文件示例，不支持ie8
                obj.preview(function(index, file, result){
                    $('#ID-upload-demo-img').attr('src', result); // 图片链接（base64）
                });

                element.progress('filter-demo', '0%'); // 进度条复位
                layer.msg('上传中', {icon: 16, time: 0});
            },
            done: function(res){
                console.log(res)
                var msg = res.msg;
                var error = res.error;
                var data = res.data;
                // 若上传失败
                if(error != 200){
                    return layer.msg('上传失败');
                }
                // 上传成功的一些操作
                $('#upload_img_url').val(data.img_url);
                $('#ID-upload-demo-text').html(''); // 置空上传失败的状态
            },
            error: function(){
                // 演示失败状态，并实现重传
                var demoText = $('#ID-upload-demo-text');
                demoText.html('<span style="color: #FF5722;">上传失败</span> <a class="layui-btn layui-btn-xs demo-reload">重试</a>');
                demoText.find('.demo-reload').on('click', function(){
                    uploadInst.upload();
                });
            },
            // 进度条
            progress: function(n, elem, e){
                element.progress('filter-demo', n + '%'); // 可配合 layui 进度条元素使用
                if(n == 100){
                    layer.msg('上传完毕', {icon: 1});
                }
            }
        });

        $("#addRecharge").on("click",function () {
            let postUrl = "{php echo createWebUrl('sms',array('op'=>'amount_recharge','themeid'=>$themeid));}";
            let formData = $("#recharge_form").serialize();
            var index = layer.load(2);
            $.ajax({
                url:postUrl,
                dataType:'json',
                method:'post',
                data:formData,
                success:function (e) {
                    console.log(e)
                    let status = e.status;
                    let result = e.result;
                    if (status!=1){
                        layer.close(index);
                        layer.msg(result.msg)
                    }else{
                        layer.msg(result.msg);
                        setTimeout(function () {
                            window.location.reload();
                        },1000);
                    }
                },
                fail:function (err) {
                    console.log(err)
                }
            })
        });

        //添加提现记录
        $("#addCashout").on("click",function () {
            let postUrl = "{php echo createWebUrl('sms',array('op'=>'amount_cashout','themeid'=>$themeid));}";
            let formData = $("#cashout_form").serialize();
            var index = layer.load(2);
            $.ajax({
                url:postUrl,
                dataType:'json',
                method:'post',
                data:formData,
                success:function (e) {
                    console.log(e)
                    let status = e.status;
                    let result = e.result;
                    if (status!=1){
                        layer.close(index);
                        layer.msg(result.msg)
                    }else{
                        layer.msg(result.msg);
                        setTimeout(function () {
                            window.location.reload();
                        },1000);
                    }
                },
                fail:function (err) {
                    console.log(err)
                }
            })
        });

    });

</script>

